<!--
 * @Author: Wushiqi
 * @Descripttion: 
 * @Date: 2020-10-18 16:10:46
 * @LastEditors: Wushiqi
 * @LastEditTime: 2020-10-18 17:01:55
-->
/*
* @Author: Wushiqi
* @Descripttion: 测试
* @Date: 2020-10-18 16:10:46
* @LastEditors: Wushiqi
* @LastEditTime: 2020-10-18 16:26:31
*/
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>

</body>
<script>
  const arr = [
    { comment_id: 1, user_id: 43, comment_date: "04-23", comment_content: "蜡笔小新很好看!", parent_id: null },
    { comment_id: 2, user_id: 19, comment_date: "04-24", comment_content: "还不错哦!很好看", parent_id: null },
    { comment_id: 3, user_id: 17, comment_date: "04-25", comment_content: "我也感觉蜡笔小新很好看", parent_id: "1" },
    { comment_id: 4, user_id: 14, comment_date: "04-26", comment_content: "我感觉机器猫更好看一点", parent_id: "3" },
    { comment_id: 5, user_id: 13, comment_date: "04-27", comment_content: "好看,已三连!", parent_id: null },
    { comment_id: 6, user_id: 21, comment_date: "04-26", comment_content: "你是机器猫的粉丝吗", parent_id: "4" },
    { comment_id: 7, user_id: 14, comment_date: "04-27", comment_content: "是的,我是机器猫的粉丝", parent_id: "6" },
    { comment_id: 8, user_id: 23, comment_date: "04-27", comment_content: "我更喜欢白嫖!", parent_id: "5" },
    { comment_id: 9, user_id: 25, comment_date: "04-28", comment_content: "你个白嫖怪", parent_id: "8" },
    { comment_id: 16, user_id: 43, comment_date: "05-03", comment_content: "这个动画片真好看", parent_id: null }
  ]

  // for循环方法
  // let arr1 = []
  // for (let i = 0; i < arr.length; i++) {
  //   if (arr[i].parent_id === null) {
  //     arr1.push(arr[i])

  //     for (let j = 0; j < arr.length; j++) {
  //       if (arr[j].parent_id === String(arr[i].comment_id)) {
  //         arr[i].child = [arr[j]]

  //         for(let p = 0; p < arr.length; p++) {
  //           if (String(arr[j].comment_id) === arr[p].parent_id) {
  //             arr[j].child = [arr[p]]
  //           }
  //         }
  //       }
  //     }
  //   }
  // }
  // console.log(arr1);

  // 递归调用方法
  function fn(temp) {
    let arr1 = []
    for (let i = 0; i <arr.length; i++) {
      if (arr[i].parent_id === temp) {
        arr[i].child = fn(String(arr[i].comment_id))
        arr1.push(arr[i])
      }
    }
    return arr1
  }
  let res = fn(null)
  console.log(res);
  
// 假如有一张纸厚度为0.0001米，折叠多少次可以超过10000米（递归）
// let a= 0.0001 // 厚度
// let num = 0 // 次数
// function fn() {
//   a *= 2 // 折叠一次
//   num ++
//   if (a < 10000) {
//     fn()
//   }
//   return num
// }
// console.log(fn());

</script>

</html>